# @Author:黄景涛
# @Date: 2023/11/10
import copy

import xmindparser
import json
from itertools import chain

"""xmind相关的处理"""


class Xmind:

    def __init__(self, file_path: str):
        self.file_path = file_path

    @property
    def read_xmind(self):
        """ 读取xmind数据 """
        with open(self.file_path, 'rb') as fn:
            content = xmindparser.xmind_to_dict(fn)
        return content[0].get('topic')

    @property
    def show_xmind(self):
        """以字典形式，展示xmind节点数据"""
        return json.dumps(self.read_xmind, ensure_ascii=False, indent=4)

    @property
    def show_output_list(self):
        for i in self.meta_data_to_list():
            print(i)
        return

    def meta_data_to_list(self):
        """回溯算法：输出节点路径的列表"""
        infos = self.read_xmind
        paths = []
        makers = []  # 存放每条用例的标记符号
        path_ = []

        def dfs_collect(d: dict):
            path_.append(d.get('title'))
            makers.append(d.get('makers'))
            if not d.get('topics'):
                tmp_path = copy.deepcopy(path_)
                tmp_makers = copy.deepcopy(makers)
                tmp_makers = list(chain(*list(i for i in tmp_makers if i is not None)))  # 清理None，并扁平化标记序列
                paths.append((tmp_path, tmp_makers))
                # paths.append(path_)
                return

            # 单层递归
            tmp_l = d.get('topics')
            for item in tmp_l:
                dfs_collect(item)
                path_.pop()
                makers.pop()

        dfs_collect(infos)
        return paths


if __name__ == '__main__':
    x = Xmind(r'../第二批次-机房机柜.xmind')
    # print(x.show_xmind)
    # print(x.output_by_list())
    x.show_output_list
    print(x.meta_data_to_list())
